package kxfang.com.main.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import kxfang.com.base.storage.MmkvHelper;
import kxfang.com.base.utils.GsonUtils;
import kxfang.com.common.adapter.ScreenAutoAdapter;
import kxfang.com.common.bean.BannerModel;
import kxfang.com.common.config.HttpConfig;
import kxfang.com.common.constant.Constant;
import kxfang.com.common.parameter.BannerPar;
import kxfang.com.common.router.RoutHub;
import kxfang.com.common.utils.HawkUtil;
import kxfang.com.common.utils.RouterUtils;
import kxfang.com.common.utils.StatusBarUtil;
import kxfang.com.http.EasyHttp;
import kxfang.com.http.callback.CallBack;
import kxfang.com.http.callback.SimpleCallBack;
import kxfang.com.http.exception.ApiException;
import kxfang.com.http.model.HttpParams;
import kxfang.com.main.R;

import com.bumptech.glide.Glide;
import com.gyf.immersionbar.BarHide;
import com.gyf.immersionbar.ImmersionBar;
import com.orhanobut.logger.Logger;

import java.util.List;

/**
 * 应用模块: 主业务模块
 * <p>
 * 类描述: 欢迎页面
 * <p>
 *
 * @author darryrzhoong
 * @since 2020-02-26
 */
public class SplashActivity extends AppCompatActivity {

    private Handler mHandler = new Handler();
    private TextView textTime;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ScreenAutoAdapter.match(this, 375.0f);
        setContentView(R.layout.main_splash_layout);
        textTime=findViewById(R.id.text_time);
        thisClick();
        ImmersionBar.with(this)
                .titleBar(findViewById(R.id.top_view))
                .hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR)
                .init();
        BannerPar par = new BannerPar();
        par.setVtype(8);
        EasyHttp.post(HttpConfig.getBanner)
                .cacheKey("test")
                .upJson(GsonUtils.toJson(par))
                .execute(new SimpleCallBack<List<BannerModel.DataBean>>() {
                    @Override
                    public void onError(ApiException e) {

                    }

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onSuccess(List<BannerModel.DataBean> o) {
                        Glide.with(SplashActivity.this)
                                .load(Constant.PHOTO_SERVER_URL.concat(o.get(0).getUrl()))
                                .into((ImageView) findViewById(R.id.ad_img));
                        mHandler.postDelayed(SplashActivity.this::startToMain, 3000);
                    }
                });
    }
    //点击事件处理
    private void thisClick() {
        textTime.setOnClickListener(v -> {
//            timer.cancel();
            if (HawkUtil.getUserId() != null) {
                MainActivity.start(this);
            } else {
                RouterUtils.navigate(RoutHub.PAGER_LOGIN);
            }

            finish();
        });
    }

    private void startToMain() {
//        if (MmkvHelper.getInstance().getMmkv().decodeBool("first",true)){
////            startActivity(new Intent(this, GuideActivity.class));
//            RouterUtils.navigate(RoutHub.PAGER_LOGIN);
//        }else {
//            MainActivity.start(this);
//        }
        if (HawkUtil.getUserId() != null) {
            MainActivity.start(this);
        } else {
            RouterUtils.navigate(RoutHub.PAGER_LOGIN);
        }

    }

    @Override
    protected void onStop() {
        super.onStop();
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //activity销毁时移除所有消息,防止内存泄漏
        mHandler.removeCallbacksAndMessages(null);
    }
}
